/**
 * DSM - Distributed Search Manager
 * Developed by Milspec Research International Pty Ltd
 * $Author: skahl $
 * $Revision: 1.5 $
 * $Date: 2007/12/05 22:28:06 $
 * (c)Copyright 2004
 * education.au limited
 * DEST
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the names education.au limited, DEST nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 * To the extent permitted by law, the copyright owners of this software and
 * its contributors:
 * (i) exclude all warranties in relation to the software; and
 * (ii) exclude liability for all losses, costs, expenses and damages arising
 * in any way from the use of the software whether arising from or in
 * relation to breach of contract, negligence or any other tort, in equity
 * or otherwise. If the software is in breach of a warranty which is
 * implied by law, the copyright owners and contributors liability is
 * limited to the replacement of the software.
 *
 * @author	gsingh
 */

package au.edu.educationau.opensource.dsm.util;

import org.apache.log4j.Logger;

/**
 * This Flog-style logging is gradually being replaced by log4j.
 * 
 * In the meantime all Flog-style logging calls are delegated to a log4j logger
 * called au.edu.educationau.opensource.dsm.Flog.
 */
public class Flog {
	static Logger logger = Logger.getLogger(Flog.class.getName());

	/** TEXT Log level - Information */
	public static final String INFO_LOG = "INFO";
	/** TEXT Log level - Debugging */
	public static final String DEBUG_LOG = "DEBUG";
	/** TEXT Log level - error */
	public static final String REPORT_LOG = "REPORT";
	/** TEXT Log level - error */
	public static final String ERROR_LOG = "ERROR";
	/** TEXT Log level - warn */
	public static final String WARN_LOG = "WARNING";
	/** TEXT Log level - Console output / Log file */
	public static final String CONSOLE_LOG = "CONSOLE";

	/** INTEGER Log level - Detailed Information */
	public static final int INFO_LOG_LEVEL = 0;
	/** INTEGER Log level - Debugging */
	public static final int DEBUG_LOG_LEVEL = 1;
	/** INTEGER Log level - Reporting, Error - with stack traces */
	public static final int REPORT_LOG_LEVEL = 2;
	/** INTEGER Log level - Error - with stack traces */
	public static final int ERROR_LOG_LEVEL = 2;
	/** INTEGER Log level - Warn - use Prod to turn of warnings */
	public static final int WARN_LOG_LEVEL = 2;
	/** INTEGER Log level - Production - no stack traces */
	public static final int PROD_LOG_LEVEL = 3;

	/** The system's log level */
	public static int LOG_LEVEL = ERROR_LOG_LEVEL;

	private static final String SOURCE_MSG_DELIM = " ";

	/**
	 * Output a console message
	 * 
	 * @param source
	 * @param msg
	 */
	public static void console(String source, String msg) {
		logger.info(source + SOURCE_MSG_DELIM + msg);
		// // FIX ME: use Log4j instance in Jahia
		// logLine(CONSOLE_LOG, source, msg);
	}

	/**
	 * Output an information message
	 * 
	 * @param source
	 * @param msg
	 */
	public static void info(String source, String msg) {
		logger.info(source + SOURCE_MSG_DELIM + msg);
		// // FIX ME: use Log4j instance in Jahia
		// if (LOG_LEVEL <= INFO_LOG_LEVEL)
		// {
		// logLine(INFO_LOG, source, msg);
		// }
	}

	/**
	 * Output a debug message
	 * 
	 * @param source
	 * @param msg
	 */
	public static void debug(String source, String msg) {
		logger.debug(source + SOURCE_MSG_DELIM + msg);
		// // FIX ME: use Log4j instance in Jahia
		// if (LOG_LEVEL <= DEBUG_LOG_LEVEL)
		// {
		// logLine(DEBUG_LOG, source, msg);
		// }
	}

	/**
	 * Output an error message
	 * 
	 * @param source
	 * @param msg
	 * @param e
	 */
	public static void error(String source, String msg, Throwable e) {
		logger.error(source + SOURCE_MSG_DELIM + msg, e);
		// // FIX ME: use Log4j instance in Jahia
		// if (LOG_LEVEL <= ERROR_LOG_LEVEL)
		// {
		// logLine(ERROR_LOG, source, msg);
		// if (null != e)
		// {
		// e.printStackTrace();
		// }
		// }
		// else if (LOG_LEVEL == PROD_LOG_LEVEL)
		// {
		// if (null != e)
		// {
		// msg += e.getMessage();
		// }
		// logLine(ERROR_LOG, source, msg);
		// }
	}

	/**
	 * Output an warning message
	 * 
	 * @param source
	 * @param msg
	 * @param e
	 */
	public static void warn(String source, String msg) {
		logger.warn(source + SOURCE_MSG_DELIM + msg);
		// // FIX ME: use Log4j instance in Jahia
		// if (LOG_LEVEL <= WARN_LOG_LEVEL)
		// {
		// logLine(WARN_LOG, source, msg);
		// }
	}

	/**
	 * Output a report message
	 * 
	 * @param source
	 * @param msg
	 */
	public static void report(String source, String msg) {
		logger.info(source + SOURCE_MSG_DELIM + msg);
		// // FIX ME: use Log4j instance in Jahia
		// if (LOG_LEVEL <= REPORT_LOG_LEVEL)
		// {
		// logLine(REPORT_LOG, source, msg);
		// }
	}

	// /**
	// * Log the line to the log file - catalina.out FIXME: use log4j
	// */
	// private static void logLine(String type, String source, String msg)
	// {
	// StringBuffer buff = new StringBuffer();
	// buff.append(DeusExMachina.LOG_PREPEND);
	// buff.append(" ");
	// buff.append(DeusExMachina.getNextTimeStamp());
	// buff.append(" ");
	// buff.append(type);
	// buff.append(" ");
	// buff.append(source);
	// buff.append(" ");
	// buff.append(msg);
	// System.err.println(buff.toString());
	// }
}
